package org.winplus.serial;

import java.io.IOException;

import android.os.Bundle;
import android.view.KeyEvent;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.TextView.OnEditorActionListener;

public class SerialDemoActivity extends SerialPortActivity{
	EditText mReception;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);

//          setTitle("Loopback test");
            mReception = (EditText) findViewById(R.id.EditTextReception);

            EditText Emission = (EditText) findViewById(R.id.EditTextEmission);
            Emission.setOnEditorActionListener(new OnEditorActionListener() {
                    public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                            int i;
                            CharSequence t = v.getText();
                            char[] text = new char[t.length()];
                            for (i=0; i<t.length(); i++) {
                                    text[i] = t.charAt(i);
                            }
                            try {
                                    mOutputStream.write(new String(text).getBytes());
                                    mOutputStream.write('\n');
                            } catch (IOException e) {
                                    e.printStackTrace();
                            }
                            return false;
                    }
            });
    }

    @Override
    protected void onDataReceived(final byte[] buffer, final int size) {
            runOnUiThread(new Runnable() {
                    public void run() {
                            if (mReception != null) {
                                    mReception.append(new String(buffer, 0, size));
                            }
                    }
            });
    }
}